<?php

class SiteController extends CController
{
	

	/**
	 * This is the default 'index' action that is invoked
	 * when an action is not explicitly requested by users.
	 */
	public function actionIndex()
	{
		// renders the view file 'protected/views/site/index.php'
		// using the default layout 'protected/views/layouts/main.php'
		$this->render('index');
	}

	/**
	 * Display the view "tree"
     * Fills the Standard tree with all data.
  	 */
	public function actionTree()
	{
        $data=$this->getDataFormatted($this->getData());
        $this->render('tree',array('data'=>$data ));
	}

    /*
     * Fills via Get Requests the tree
     */
    public function actionTreeFill()
    {
        if(!isset($_GET['root']))
            $personId='source';
        else
            $personId=$_GET['root'];
        $dataTree=array();

        $persons=($personId=='source')?$this->getData():$this->recursiveSearch($personId, $this->getData());
        if(is_array($persons))
        {

            foreach($persons as $parent)
            {
                $dataTree[]=$this->formatData($parent);
            }
        }
        echo CTreeView::saveDataAsJson($dataTree);
    }

    
    /*
     * Used by Async Tree
     * @param id the Id of the child
     * @param root the node to test
     * @return the Familly Member
     */
    protected function recursiveSearch($id,$array)
    {
        
       if(is_array($array))
        {
            foreach($array as $person)
            {
                if($person['id']==$id)
                    return $person["parents"];
                else
                {
                    $r=$this->recursiveSearch($id,$person["parents"]);
                    if($r!==null)
                         return $r;

               }
            }
        }
        return null;
    }

    /*
     * @return data for the tree
     */
    protected function formatData($person)
    {
      return array('text'=>$person['name'],'id'=>$person['id'],'hasChildren'=>isset($person['parents']));
    }

    /*
     * @return data formated for the standard tree
     */
    protected function getDataFormatted($data)
    {

        foreach($data as $k=>$person)
        {
            $personFormatted[$k]=$this->formatData($person);
            $parents=null;
            if(isset($person['parents']))
            {
                $parents=$this->getDataFormatted($person['parents']);
                $personFormatted[$k]['children']=$parents;
            }
        }
        return $personFormatted;
    }
   
    /*
     * @return all the data
     */
    protected function getData()
    {
        $data=array(
                  array("id"=>1,"name"=>"John","parents"=>
                        array(
                                array("id"=>10,"name"=>"Mary","parents"=>
                                    array(
                                        array("id"=>100,"name"=>"Jane","parents"=>
                                            array(
                                                array("id"=>1000,"name"=>"Helene",),
                                                array("id"=>1001,"name"=>"Peter",),
                                            )
                                        ),
                                        array("id"=>101,"name"=>"Richard","parents"=>
                                            array(
                                                array("id"=>1010,"name"=>"Lisa",),
                                                array("id"=>1011,"name"=>"William",),
                                            )
                                        ),
                                    ),
                                ),
                                array("id"=>11,"name"=>"Derek","parents"=>
                                    array(
                                        array("id"=>110,"name"=>"Julia"),
                                        array("id"=>111,"name"=>"Christian","parents"=>
                                            array(
                                                array("id"=>1110,"name"=>"Deborah",),
                                                array("id"=>1111,"name"=>"Marc",),
                                            ),
                                        ),
                                    ),
                                ),
                        ),
                    ),
                 );

        return $data;
    }
}